package main

import (
	"fmt"
	"strings"
)

// Our client provided us with a list of user IDs of customers who bought something on a landing page
//after clicking one of their ads:

// # Each user completed 1 purchase.
// completed_purchase_user_ids = [
//   "3123122444","234111110", "8321125440", "99911063"]

// And our ops team provided us with some raw log data from our ad server showing every time a user clicked on one of our ads:
// ad_clicks = [
//  #"IP_Address,Time,Ad_Text",
//  "122.121.0.1,2016-11-03 11:41:19,Buy wool coats for your pets",
//  "96.3.199.11,2016-10-15 20:18:31,2017 Pet Mittens",
//  "122.121.0.250,2016-11-01 06:13:13,The Best Hollywood Coats",
//  "82.1.106.8,2016-11-12 23:05:14,Buy wool coats for your pets",
//  "92.130.6.144,2017-01-01 03:18:55,Buy wool coats for your pets",
//  "92.130.6.145,2017-01-01 03:18:55,2017 Pet Mittens",
//]

//The client also sent over the IP addresses of all their users.

//all_user_ips = [
//  #"User_ID,IP_Address",
//   "2339985511,122.121.0.155",
//  "234111110,122.121.0.1",
//  "3123122444,92.130.6.145",
//  "39471289472,2001:0db8:ac10:fe01:0000:0000:0000:0000",
//  "8321125440,82.1.106.8",
//  "99911063,92.130.6.144"
//]

// Write a function to parse this data, determine how many times each ad was clicked,
//then return the ad text, that ad's number of clicks, and how many of those ad clicks
//were from users who made a purchase.

// Expected output:
// Bought Clicked Ad Text
// 1 of 2  2017 Pet Mittens
// 0 of 1  The Best Hollywood Coats
// 3 of 3  Buy wool coats for your pets

// map 1: ip tp user
// map 2: ad and who clicked
func adContersionRate(purchaseUserIds, adClicks, userIps []string) []string {
	res := []string{}
	ipToUserId := make(map[string]string)
	whoClicksAds := make(map[string][]string)
	for _, userIpRecord := range userIps {
		ip := strings.Split(userIpRecord, ",")[1]
		userId := strings.Split(userIpRecord, ",")[0]
		ipToUserId[ip] = userId
	}

	for _, adClickRecord := range adClicks {
		ad := strings.Split(adClickRecord, ",")[2]
		userId := "unknown"
		if _, ok := ipToUserId[strings.Split(adClickRecord, ",")[0]]; ok == true {
			userId = ipToUserId[strings.Split(adClickRecord, ",")[0]]
		}
		whoClicksAds[ad] = append(whoClicksAds[ad], userId)
	}
	// fmt.Println(whoClicksAds)
	// calculate the rate

	for ad, userIds := range whoClicksAds {
		count := 0
		for _, userId := range userIds {
			if stringInSlice(userId, purchaseUserIds) != -1 {
				count += 1
			}
		}
		s := fmt.Sprintf("%v of %v  %s", count, len(userIds), ad)
		res = append(res, s)
	}

	return res
}

func stringInSlice(a string, list []string) int {
	for i, b := range list {
		if b == a {
			return i
		}
	}
	return -1
}

func main() {
	completed_purchase_user_ids := []string{"3123122444", "234111110", "8321125440", "99911063"}

	all_user_ips := []string{"2339985511,122.121.0.155",
		"234111110,122.121.0.1",
		"3123122444,92.130.6.145",
		"39471289472,2001:0db8:ac10:fe01:0000:0000:0000:0000",
		"8321125440,82.1.106.8",
		"99911063,92.130.6.144",
	}

	ad_clicks := []string{
		"122.121.0.1,2016-11-03 11:41:19,Buy wool coats for your pets",
		"96.3.199.11,2016-10-15 20:18:31,2017 Pet Mittens",
		"122.121.0.250,2016-11-01 06:13:13,The Best Hollywood Coats",
		"82.1.106.8,2016-11-12 23:05:14,Buy wool coats for your pets",
		"92.130.6.144,2017-01-01 03:18:55,Buy wool coats for your pets",
		"92.130.6.145,2017-01-01 03:18:55,2017 Pet Mittens",
	}

	fmt.Println(adContersionRate(completed_purchase_user_ids, ad_clicks, all_user_ips))
}
